package com.fpt.edu.vn.h2tv.service;

import java.util.ArrayList;
import java.util.List;

import com.fpt.edu.vn.h2tv.common.ElementName;
import com.fpt.edu.vn.h2tv.dao.EntityManagerHelper;
import com.fpt.edu.vn.h2tv.dao.ITbOrderDAO;
import com.fpt.edu.vn.h2tv.dao.TbOrderDAO;
import com.fpt.edu.vn.h2tv.dao.entity.TbOrder;
import com.fpt.edu.vn.h2tv.dto.OrderResponse;
import com.fpt.edu.vn.h2tv.dto.Orders;

public class OrderDetailService {

    public static OrderResponse listAllOrder() {
        ITbOrderDAO dao = new TbOrderDAO();
        List<TbOrder> tbOrderList = dao.findAll();
        OrderResponse response = new OrderResponse();
        List<Orders> orderList = new ArrayList<Orders>();

        if (tbOrderList != null && tbOrderList.size() > 0) {
            for (TbOrder order : tbOrderList) {
                Orders orderDTO = new Orders();
                orderDTO.setOrderId(order.getOrderid().longValue());
                orderDTO.setUserId(order.getTbAccount().getUserid().longValue());
                orderDTO.setReceiverName(order.getReceivepersonname());
                orderDTO.setReceivePhone(order.getReceivephonenumber());
                orderDTO.setReceiveAddress(order.getReceiveaddress());
                orderDTO.setOrderDate(order.getOrderdate().toString());
                orderDTO.setShippingMethod(order.getShippingmethodid().shortValue());
                orderDTO.setPaymentMethod(order.getPaymentmethodid().shortValue());
                orderDTO.setTotalPrice(order.getTotalprice().doubleValue());
                orderDTO.setStatus(order.getStatus().intValue());

                orderList.add(orderDTO);
            }
            response.setStatus(ElementName.SUCCESS);
        } else {
            response.setStatus(ElementName.NO_DATA);
        }
        response.setOrder(orderList);
        return response;
    }

    public static OrderResponse searchOrderById(Long orderId) {
        ITbOrderDAO dao = new TbOrderDAO();
        
        List<TbOrder> tbOrderList = dao.findByOrderId(orderId);
        OrderResponse response = new OrderResponse();
        List<Orders> orderList = new ArrayList<Orders>();

        if (tbOrderList != null && tbOrderList.size() > 0) {
            for (TbOrder order : tbOrderList) {
                Orders orderDTO = new Orders();
                orderDTO.setOrderId(order.getOrderid().longValue());
                orderDTO.setUserId(order.getTbAccount().getUserid().longValue());
                orderDTO.setReceiverName(order.getReceivepersonname());
                orderDTO.setReceivePhone(order.getReceivephonenumber());
                orderDTO.setReceiveAddress(order.getReceiveaddress());
                orderDTO.setOrderDate(order.getOrderdate().toString());
                orderDTO.setShippingMethod(order.getShippingmethodid().shortValue());
                orderDTO.setPaymentMethod(order.getPaymentmethodid().shortValue());
                orderDTO.setTotalPrice(order.getTotalprice().doubleValue());
                orderDTO.setStatus(order.getStatus().intValue());
                
                orderList.add(orderDTO);
            }
            response.setStatus(ElementName.SUCCESS);
        } else {
            response.setStatus(ElementName.NO_DATA);
        }
        response.setOrder(orderList);
        return response;
    }

    public static String updateOrder(Long orderId, int status) {
        String noti = "";
        ITbOrderDAO dao = new TbOrderDAO();
        TbOrder order = dao.findById(orderId);
        if (order != null) {
            EntityManagerHelper.beginTransaction();
            order.setStatus(status);
            dao.update(order);
            EntityManagerHelper.commit();
            noti = "update orderId " + orderId + " successful!";
        } else {
            noti = "update orderId " + orderId + " failed!";
        }

        return noti;
    }
    
    public static String deleteOrder(Long orderId) {
        String noti = "";
        ITbOrderDAO dao = new TbOrderDAO();
        TbOrder order = dao.findById(orderId);
        if (order != null) {
            EntityManagerHelper.beginTransaction();           
            dao.delete(order);
            EntityManagerHelper.commit();
            noti = "delete orderId " + orderId + " successful!";
        } else {
            noti = "delete orderId " + orderId + " failed!";
        }

        return noti;
    }

    public static Object getOrderDetailById(Long id) {
        ITbOrderDAO dao = new TbOrderDAO();
        TbOrder tbOrder = dao.findById(id);
        OrderResponse response = new OrderResponse();        
        Orders orderDTO = new Orders();
        if (tbOrder != null) {
            orderDTO.setOrderId(tbOrder.getOrderid().longValue());
            orderDTO.setUserId(tbOrder.getTbAccount().getUserid().longValue());
            orderDTO.setReceiverName(tbOrder.getReceivepersonname());
            orderDTO.setReceivePhone(tbOrder.getReceivephonenumber());
            orderDTO.setReceiveAddress(tbOrder.getReceiveaddress());
            orderDTO.setOrderDate(tbOrder.getOrderdate().toString());
            orderDTO.setShippingMethod(tbOrder.getShippingmethodid().shortValue());
            orderDTO.setPaymentMethod(tbOrder.getPaymentmethodid().shortValue());
            orderDTO.setTotalPrice(tbOrder.getTotalprice().doubleValue());
            orderDTO.setStatus(tbOrder.getStatus().intValue());

            response.setStatus(ElementName.SUCCESS);
        } else {
            response.setStatus(ElementName.NO_DATA);
        }
        
        return orderDTO;
    }
    
}
